package cn.hbu.zlfw.common.util;

import java.io.Serializable;
import java.util.List;
/**
 * 
 *@ClassName:PageModel.java
 *@Description:  封装分页信息
 *@Author:lxt<839376636@qq.com>
 *@Date:2017年10月6日下午6:44:53
 *@Version:1.1.0
 */
public class PageModel<T> implements Serializable{
	
	private static final long serialVersionUID = 6273173973590328221L;

	private static final Integer PAGE_SIZE = 10;//每页显示条数
	
	//当前页
    private int currentPage; 
    //每页显示条数
    private int pageSize=PAGE_SIZE;  
    //总条数
    private int totalCount;  
    //总页数
    private int totalPage;  
    //数据集合  
    private List<T> list;
    //分页样式设计
    private int startPage;	//开始页码
    	
    private int endPage;	//结束页码
    
    private String url;		//分页数据请求url
    
    public PageModel() {
		super();
	}
    /*
     * 构造函数，自定义页码
     */
	public PageModel(int currentPage, int pageSize, int totalCount) {
		super();
		setCurrentPage(currentPage);
		setPageSize(pageSize);
		setTotalCount(totalCount);
		adjustPageNo();
	}

	/*
	 * 构造函数默认页码
	 */
	public PageModel(int currentPage, int totalCount) {
		super();
		setCurrentPage(currentPage);
		setTotalCount(totalCount);
		adjustPageNo();
		
	}

	/*
     * 第一条记录的位置
     */
    public int getFirstResult() {
		return (getCurrentPage()-1)*getPageSize();
	}
    /*
     * 获取当前页码
     */
	public int getCurrentPage() {
		if(currentPage<1) {
			return 1;
		}
		if(currentPage>getTotalPage()) {
			return getTotalPage();
		}
		return currentPage;
	}
	/*
	 * 当前页赋值
	 */
	public void setCurrentPage(int currentPage) {
		if(currentPage<1) {
			this.currentPage = 1;
		}else {
			this.currentPage = currentPage;
		}
		
	}
	/*
	 * 获取每页显示条数
	 */
	public int getPageSize() {
		return pageSize<1 ? PAGE_SIZE :pageSize;
	}
	
	public void setPageSize(int pageSize) {
		if(pageSize<1) {
			this.pageSize = PAGE_SIZE;
		}else {
			this.pageSize = pageSize;
		}
		
	}

	public int getTotalCount() {
		return totalCount<1 ? 0 :totalCount;
	}
	
	public void setTotalCount(int totalCount) {
		if(totalCount<1) {
			this.totalCount = 0;
			this.currentPage=1;
		}else {
			this.totalCount = totalCount;
		}
		//分页样式页码处理
		int totalPage = getTotalPage();
		//计算开始和结束页码:9个页码
		if(totalPage> 9){
			//超过9页
			startPage = this.currentPage-4;
			endPage = this.currentPage+4;
			if(startPage<1){
				startPage = 1;
				endPage = 9;
			}
			if(endPage> totalPage){
				endPage = totalPage;
				startPage = endPage-8;
			}
		}else{
			//没有9页
			startPage = 1;
			endPage = totalPage;
		}
	}

	public int getTotalPage() {
		int totalPage = totalCount / pageSize;
		if (totalPage == 0 || totalCount % pageSize != 0) {
			totalPage++;
		}
		return totalPage;
	}

	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	
	/*
	 *判断是否是第一页
	 */
	public boolean isFirstPage() {
		return currentPage<=1;
	}
	/*
	 * 判断是够是最后一页
	 */
	public boolean isLastPage() {
		return currentPage >=getTotalPage();
	}
	/*
	 * 调整页码，使不超过最大页数
	 */
	public void adjustPageNo() {
		if (currentPage == 1) {
			return;
		}
		int tp = getTotalPage();
		if (currentPage > tp) {
			currentPage = tp;
		}
	}
	public int getStartPage() {
		return startPage;
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	@Override
	public String toString() {
		return "PageModel [currentPage=" + currentPage + ", pageSize=" + pageSize + ", totalCount=" + totalCount
				+ ", totalPage=" + totalPage + ", list=" + list + "]";
	}
    
}
